<?php
#注册插件
RegisterPlugin("SearchKeywords","ActivePlugin_SearchKeywords");

function ActivePlugin_SearchKeywords() {
    global $zbp;
    if($zbp->CheckPlugin('baidu_search')){
        Add_Filter_Plugin('Filter_Plugin_Cmd_Begin','SearchKeywords_Baidu_Insert_Sql');
    }else{
        Add_Filter_Plugin('Filter_Plugin_Search_Begin','SearchKeywords_Insert_Sql');
    }
}
function InstallPlugin_SearchKeywords() {
    global $zbp;
    SearchKeywords_CreateTable();
}
function UninstallPlugin_SearchKeywords() {}
///////////////////////////////////////////////////////////////////////
function SearchKeywords_TimeAgo($ptime){
	$ptime = strtotime($ptime);
	$etime = time() - $ptime;
	if($etime < 1) return '刚刚';
	$interval = array (
		12 * 30 * 24 * 60 * 60=>'年前<time class="datetime"> ('.date('Y-m-d', $ptime).')</time>',
		30 * 24 * 60 * 60=>'个月前<time class="datetime"> ('.date('m-d', $ptime).')</time>',
		7 * 24 * 60 * 60=>'周前<time class="datetime"> ('.date('m-d', $ptime).')</time>',
		24 * 60 * 60=>'天前',
		60 * 60=>'小时前',
		60=>'分钟前',
		1=>'秒前'
	);
	foreach ($interval as $secs => $str) {
		$d = $etime / $secs;
		if ($d >= 1) {
			$r = round($d);
			return $r . $str;
		}
	};
}
///////////////////////////////////////////////////////////////////////
function SearchKeywords_CreateTable(){
	global $zbp;
	$s=$zbp->db->sql->CreateTable($GLOBALS['SearchKeywords_Table'],$GLOBALS['SearchKeywords_DataInfo']);
	$zbp->db->QueryMulit($s);
}
///////////////////////////////////////////////////////////////////////
function SearchKeywords_EmptyCode(){
	global $zbp;
	$s=$zbp->db->sql->DelTable($GLOBALS['SearchKeywords_Table'],'');
	$zbp->db->QueryMulit($s);
	$zbp->SetHint('good','所有数据已删除！');
}
///////////////////////////////////////////////////////////////////////
$SearchKeywords_Table='%pre%SearchKeywords';
$SearchKeywords_DataInfo=array(
	'ID'=>array('t_ID','integer','',0),
	'Keyword'=>array('t_Keyword','string',255,''),
	'IP'=>array('t_IP','string',255,''),
	'UA'=>array('t_UA','string',255,''),
	'Time'=>array('t_Time','string',255,'')
);
///////////////////////////////////////////////////////////////////////
function SearchKeywords_Insert_Sql(){
    global $zbp;
	global $SearchKeywords_Table;
	global $SearchKeywords_DataInfo;
    $keyword=$_GET['q'];
    $ip=GetGuestIP();
    $ua=GetGuestAgent();
    $time=time();
	$DataArr = array(
		//'t_ID'=>$keyword,
		't_Keyword'=>$keyword,
		't_IP'=>$ip,
		't_UA'=>GetGuestAgent(),
		't_Time'=>$time
	);
	$sql = $zbp->db->sql->Insert($SearchKeywords_Table,$DataArr);
	$zbp->db->Insert($sql);
}
///////////////////////////////////////////////////////////////////////
function SearchKeywords_Baidu_Insert_Sql(){
    global $zbp;
	global $SearchKeywords_Table;
	global $SearchKeywords_DataInfo;
	global $action;
    if($action=='search'){
        $keyword=trim(strip_tags(GetVars('q', 'POST')));
        $ip=GetGuestIP();
        $ua=GetGuestAgent();
        $time=time();
    	$DataArr = array(
    		//'t_ID'=>$keyword,
    		't_Keyword'=>$keyword,
    		't_IP'=>$ip,
    		't_UA'=>GetGuestAgent(),
    		't_Time'=>$time
    	);
    	$sql = $zbp->db->sql->Insert($SearchKeywords_Table,$DataArr);
    	$zbp->db->Insert($sql);
	}
}